package com.bjut.dao.impl;

import java.util.List;

import com.bjut.dao.DepartmentDao;
import com.bjut.domain.Department;

public class DepartmentDaoImpl extends HRglxtHibernateDaoSupport implements DepartmentDao{

	public void delete(Department department) {
		getHibernateTemplate().save(department);
	}

	public void delete(int id) {
		getHibernateTemplate().delete(get(id));
	}

	public Department findDepartmentByName(String name) {
		List result=getHibernateTemplate().find("from Department as d where d.name = ?" , 
				new String[]{name});
		if (result != null && result.size() > 0)
			return (Department) result.get(0);
		else 
			return null;
	}

	public Department get(int id) {
		return (Department) getHibernateTemplate().get(Department.class, id);
	}

	public void save(Department department) {
		getHibernateTemplate().save(department);
	}

	public void update(Department department) {
		getHibernateTemplate().update(department);
		
	}

	public List<Department> findAll() {
		return getHibernateTemplate().find("from Department as d order by d.id");
	}
	
	public List<Department> findAllByPage(int pageNo, int pageSize) {
		if (pageNo < 1)
		{
			return null;
		}
		int offset = (pageNo - 1) * pageSize;
		return findByPage("from Department as d order by d.id", offset , pageSize);
	}

	public long getCount() {
		List departmentList = getHibernateTemplate().find("select count(d.id) from Department as d");
		if (departmentList == null)
		{
			return 0;
		}
		return (Long)departmentList.get(0);
	}

}
